package com.xuexiang.xuidemo.page.base;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

/**
 * Page属性获取工具类
 *
 * @since 2021-03-25
 */
public class PageUtils {
    private PageUtils() {
    }

    /**
     * 跳转页面
     *
     * @param ability 上下文
     * @param pageClz 要跳转的ability页面class
     */
    public static void jump(Ability ability, Class pageClz) {
        Page annotation = getPage(pageClz);
        Intent intent = new Intent();
        intent.setOperation(new Intent.OperationBuilder()
            .withBundleName(annotation.bundleName())
            .withAbilityName(annotation.ability())
            .withAction(annotation.action())
            .build());
        ability.startAbility(intent);
    }

    /**
     * 获取Class的Page注解
     *
     * @param clz 对应的class
     * @return class对应的注解
     */
    public static Page getPage(Class clz) {
        Page annotation = (Page) clz.getAnnotation(Page.class);
        if (annotation == null) {
            throw new IllegalArgumentException("The page " + clz.getSimpleName() + " must be set @page()");
        }
        return annotation;
    }

    /**
     * 获取clz配置的Action
     *
     * @param clz page对应的class
     * @return page.action
     */
    public static String getAction(Class clz) {
        return getPage(clz).action();
    }

    /**
     * 获取clz配置的副标题
     *
     * @param clz page对应的desc
     * @return page.action
     */
    public static String getSubTitle(Class clz) {
        return getPage(clz).subTitle();
    }

    /**
     * 获取clz配置的Name
     *
     * @param clz page对应的class
     * @return page.name
     */
    public static String getName(Class clz) {
        return getPage(clz).name();
    }
}
